package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.common.utils.StringUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.ServiceItemsModel;
import com.slwish.xty.entity.SubscribeModel;
import com.slwish.xty.mvp.contract.SubscribeEditContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.litepal.crud.DataSupport;


/**
 * 模块说明：预约Presenter
 *
 * @author 陈浩
 * @date 2017/12/1
 */
public class SubscribeEditPresenter extends BasePresenter<SubscribeEditContract.View> implements SubscribeEditContract.Presenter {
    public SubscribeEditPresenter(SubscribeEditContract.View view) {
        super(view);
    }

    /**
     * 提交数据
     *
     * @param context
     * @param aid           当前登录人id
     * @param mid           商户id
     * @param linkman       联系人
     * @param phone         联系方式
     * @param resideTime    入住时间
     * @param departureTime 离开时间
     * @param dinersNum     就餐人数
     * @param stayNum       住宿人数
     * @param remarks       备注
     * @param services      服务项目
     */
    @Override
    public void submitSubscribe(Context context, String aid
            , String mid, String linkman, String phone
            , String resideTime, String departureTime
            , String dinersNum, String stayNum, String remarks, String services) {

        if (StringUtils.isEmpty(aid)) {
            setError(R.string.userid_failed_text);
            return;
        }
        if (StringUtils.isEmpty(mid)) {
            setError(R.string.mid_failed_text);
            return;
        }
        if (StringUtils.isEmpty(resideTime)) {
            setError(R.string.toast_chose_start_time_text);
            return;
        }
        if (StringUtils.isEmpty(departureTime)) {
            setError(R.string.toast_chose_end_time_text);
            return;
        }
        if (StringUtils.isEmpty(linkman)) {
            setError(R.string.input_lxr_hint);
            return;
        }
        if (StringUtils.isEmpty(phone)) {
            setError(R.string.input_lxfs_hint);
            return;
        }
        if (!StringUtils.isMobileExact(phone)) {
            setError(R.string.input_right_phone_number_hint);
            return;
        }

        if (StringUtils.isEmpty(dinersNum)) {
            setError(R.string.toast_zsrs_not_null_text);
            return;
        }
        if (StringUtils.isEmpty(stayNum)) {
            setError(R.string.toast_ycrs_not_null_text);
            return;
        }
        if (StringUtils.isEmpty(services)) {
            setError(R.string.toast_fwxm_not_null_text);
            return;
        }
        EasyHttp.post(UrlConfig.SUBMIT_SUBSCRIBE_URL)
                .params("aid", aid)
                .params("mid", mid)
                .params("linkman", linkman)
                .params("phone", phone)
                .params("resideTime", resideTime)
                .params("departureTime", departureTime)
                .params("dinersNum", dinersNum)
                .params("stayNum", stayNum)
                .params("remarks", remarks)
                .params("services", services)

                .execute(new ProgressDialogCallBack<String>(showProgressDialog(context)) {
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "s----->" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0")) {
//                                setError(resultMessage);
                                return;
                            }
                            SubscribeModel model = JSON.parseObject(data, SubscribeModel.class);
                            if (model != null && getView() != null) {
                                DataSupport.saveAll(model.getServiceList());
                                model.saveOrUpdate("orderid = ?", model.getOrderId() + "");
//                                List<SubscribeModel> subscribeModels = DataSupport.where("orderid = ?", model.getOrderId() + "").find(SubscribeModel.class);
//                                ServiceList first = DataSupport.findFirst(ServiceList.class);
                                getView().submitSuccess(model);
                            }
                        } catch (Exception e) {
                            setError(R.string.input_failed_text);
                        }
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        setError(R.string.input_failed_text);
                    }
                });
    }

    /**
     * 查询服务项目
     */
    @Override
    public void queryServiceItems() {
        EasyHttp.post(UrlConfig.QUERYSERVICEITEMS_URL)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        setError(R.string.query_server_option_failed);
                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json--->" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            ServiceItemsModel model = JSON.parseObject(json, ServiceItemsModel.class);
                            SubscribeEditContract.View mview = getView();
                            if (model != null && mview != null) {
                                mview.queryServiceSuccess(model);
                            }
                        } catch (Exception e) {
                            setError(R.string.query_server_option_failed);
                        }
                    }
                });
    }
}
